package com.huazi.www.yang.fchua.dao.Impl;

import com.huazi.www.yang.fchua.dao.BillDao;
import com.huazi.www.yang.fchua.entity.Medicine;
import com.huazi.www.yang.fchua.entity.Prescription;
import com.huazi.www.yang.fchua.uilt.BaseDao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * dao实现类
 */
public class BillDaoImpl extends BaseDao implements BillDao {

    ResultSet rs = null;

    //查询总记录数
    @Override
    public int totalCount(String name) {
        //总记录数
        int totalCount = 0;
        //拼接查询语句
        StringBuffer str = new StringBuffer();

        str.append("select count(1) from doctor_medicine");
        str.append(" where state = 0");
        //条件  根据姓名模糊查询
        str.append(" and username like concat('%',?,'%')");
        str.append(" GROUP BY userid,time");
        str.append(" LIMIT 0,1");

        rs = executeselect(str.toString(), name);
        try {
            rs.next();
            totalCount = rs.getInt(1);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return totalCount;
    }

    //分页数据
    @Override
    public List<Medicine> list(String name, int start, int pageSize) {
        //分页数据集合
        List<Medicine> list = new ArrayList<>();
        //条件  根据姓名模糊查询
        StringBuffer str = new StringBuffer();
        str.append("select m.id,m.userid,m.username,count(m.medicineid),m.time,i.doctorname from doctor_medicine as m,doctor_information as i");
        str.append(" where m.doctorid = i.doctorId");
        str.append(" and m.state = 0");
        //条件  根据姓名模糊查询
        str.append(" and username like concat('%',?,'%')");
        str.append(" GROUP BY m.userid,m.time");
        str.append(" limit ?,?");
        //查询
        rs = executeselect(str.toString(), name, start, pageSize);
        try {
            while (rs.next()) {
                int id = rs.getInt(1);    //编号
                int userId = rs.getInt(2);  //用户编号
                String userName = rs.getString(3);  //用户姓名
                int count = rs.getInt(4);   //药品数量
                String time = rs.getString(5);    //开药时间
                String dName = rs.getString(6);   //医生姓名
                Medicine medicine = new Medicine(id, userId, userName, count, time, dName);
                list.add(medicine);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return list;
    }

    @Override
    public List<Medicine> list(String userid, String date) {
        List<Medicine> list = new ArrayList<>();
        String sql = "select m.id,d.drugname,d.price,m.sum,m.type from drug_remedy d,doctor_medicine m where d.id = m.medicineid and m.userid = ? and m.time = ? ";
        rs = executeselect(sql, userid, date);
        try {
            while (rs.next()) {
                int id = rs.getInt(1);    //订单编号
                String drugName = rs.getString(2);    //药品名称
                double price = rs.getDouble(3);       //药品价格
                int sum = rs.getInt(4);     //开药数量
                String type = rs.getString(5);    //用法用量
                Medicine medicine = new Medicine(id, drugName, price, sum, type);
                list.add(medicine);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return list;
    }

    @Override
    public int updates(int id) {
        String sql = "update doctor_medicine set state = 1 where id = ?";
        return executeupdate(sql, id);
    }

    /**
     * 查询总记录数
     *
     * @param
     * @return
     */
    @Override
    public Integer totalCount() {
        String sql = "SELECT COUNT(1) FROM doctor_medicine where doctor_medicine.state=0";
//        if(name!=null&&!name.equals("")){
//            sql+=" and doctor_medicine.username LIKE CONCAT('%',?,'%')";
//            rs=executeselect(sql,name);
//        }else {
        rs = executeselect(sql);
//        }
        Integer count = 0;
        try {
            while (rs.next()) {
                count = rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return count;
    }


    /**
     * 分页查询药缴费单
     *
     * @param name     姓名模糊查询
     * @param start    每页第一条数据的下标
     * @param pageSize 每页显示行数
     * @return
     */
    @Override
    public List<Medicine> yaoSelect(String name, Integer start, Integer pageSize) {
        List<Medicine> list = new ArrayList<>();
//        String sql="SELECT doctor_medicine.id,doctor_medicine.userid,doctor_medicine.username,drug_remedy.drugname,doctor_medicine.sum,doctor_medicine.time,doctor_information.doctorname,doctor_medicine.type,doctor_medicine.state FROM `doctor_medicine`,doctor_information,drug_remedy\n" +
//                "where doctor_medicine.medicineid=drug_remedy.id and doctor_medicine.doctorid=doctor_information.doctorId AND doctor_medicine.state=0 ";
//        if(name!=null&&!name.equals("")){
//            sql+=" and doctor_medicine.username LIKE CONCAT('%',?,'%')  LIMIT ?,?";
//            rs=executeselect(sql,name,start,pageSize);
//        }else {
//            sql+=" LIMIT ?,? ";
//            rs=executeselect(sql,start,pageSize);
//        }
//        try {
//            while (rs.next()){
//                Integer id=rs.getInt(1);//编号
//                Integer userid=rs.getInt(2);//用户id
//                String  username=rs.getString(3);//用户姓名
//                String drugname=rs.getString(4);//药名
//                Integer sum=rs.getInt(5);//数量
//                String time=rs.getString(6);//时间
//                String doctorname=rs.getString(7);//医生名
//                String type=rs.getString(8);//吃药条件
//                String state="未缴费";
////                Integer state=rs.getInt(9);//状态
////
////                if(state==0){
////
////                }
//                Medicine medicine=new Medicine(id,userid,username,drugname,sum,time,doctorname,type,state);
//                list.add(medicine);
//            }
//        } catch (SQLException throwables) {
//            throwables.printStackTrace();
//        }
        StringBuffer str = new StringBuffer();
        str.append("");
        return list;
    }

    /**
     * 插入药单
     *
     * @param pre 药单实体对象
     * @return
     */
    @Override
    public int insert(Prescription pre) {
        String sql = "insert into frontdesk_money(frontdesk_money.petientid,frontdesk_money.drugname,frontdesk_money.drugprice) VALUES(?,?,?)";
        int count = executeupdate(sql, pre.getPetientid(), pre.getDrugname(), pre.getDrugprice());
        String str = "INSERT into `consumeinfo` (`cardNo`,`consumeType`,`drugType`,`money`) VALUES(?,'药品','前台缴费',?)";
        executeupdate(str, pre.getPetientid(), pre.getDrugprice());
        return count;
    }

    /**
     * 根据药id查价格
     *
     * @param drugname
     * @return
     */

    public double selectPrice(String drugname) {
        String sql = "select drug_remedy.price from drug_remedy where drug_remedy.drugname=?";
        rs = executeselect(sql, drugname);
        double count = 0;
        try {
            while (rs.next()) {
                count = rs.getDouble(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return count;
    }

    /**
     * 修改药单状态
     *
     * @return
     */
    @Override
    public int update(Integer userId) {
        String sql = "update doctor_medicine set doctor_medicine.state=1 where doctor_medicine.userid=?";
        int count = executeupdate(sql, userId);
        return count;
    }

    /**
     * 用户余额查询
     *
     * @param id 用户id
     * @return
     */
    @Override
    public double selectUserMoney(Integer id) {

        String sql = "SELECT frontdesk_card.money FROM `frontdesk_card` where frontdesk_card.id=?";
        rs = executeselect(sql, id);
        double price = 0;
        try {
            while (rs.next()) {
                price = rs.getDouble(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return price;
    }

    /**
     * 修改余额
     *
     * @param id 用户id
     * @return
     */
    @Override
    public int updateUserMoney(double money, Integer id) {
        String sql = "update frontdesk_card set frontdesk_card.money=frontdesk_card.money-? where frontdesk_card.id=?";
        int coutn = executeupdate(sql, money, id);
        return coutn;
    }

}
